<?php

class MembersController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/admin/main';

	public $email;
	public $username;

	public function init()
	{
		Yii::app()->user->loginUrl = array('/admin/site/login');
		parent::init();
	}

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('login','logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update', 'index', 'view', 'edit', 'new'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	public function actionCreate()
	{
		if(isset($_POST['member']))
		{
			$model=new Users;
			$data=$_POST['member'];
			$model->attributes=$data;
      $model->password='default';
			if($model->save())
			{
        Yii::app()->user->setFlash('success', 'Success');
        Yii::app()->user->setFlash('content', 'You have created a new member.');
				$this->redirect(array('index?member='.$model->member_id.''));
			}
			else
			{
        Yii::app()->user->setFlash('error', 'Error');
        Yii::app()->user->setFlash('content', 'Please try again.');
				$this->redirect(array('new','?id='.$model->member_id.''));
			}
		}
	}

	public function actionUpdate($id)
	{
		if(isset($_POST['member']))
		{
			$model=Users::model()->findByPk($_GET['id']);
			$model->attributes=$_POST['member'];
      
			if($model->save())
			{
        Yii::app()->user->setFlash('success', 'Success');
        Yii::app()->user->setFlash('content', 'You have updated member\'s info.');
				$this->redirect(array('index?member='.$model->member_id.''));
			}
		}

		$this->render('update',array(
			'member'=>$model,
		));
	}

	public function actionDelete()
	{
    $user = Users::model()->find('id = :id',array(':id'=>$_GET['id']));
    $member_id = $user->member_id;
    $user->delete();
    
    $school = School::model()->find('user_id = :user_id', array(':user_id' => $_GET['id']));
    $school->delete();
    
    Yii::app()->user->setFlash('success', 'Success');
    Yii::app()->user->setFlash('content', 'You have deleted a member.');
    $this->redirect(bu('admin/members/index?member='.$member_id));
	}

	public function actionIndex()
	{
		$members = Users::model()->with('member')->findAll('member_id = :member_id',array(':member_id' => $_GET['member']));
    
    $invoices = Invoices::model()->findAll('status = 0');
		$this->render('index',array(
			'members'=>$members,
      'invoices' => $invoices
		));
	}

	public function actionNew()
	{
		$this->render('new');
	}

	public function actionEdit()
	{
		$member = Users::model()->find('id = :id', array(':id' => $_GET['id']));

    $forms = Forms::model()->find('member_id = :member_id', array(':member_id' => $member->member_id));
		$this->render('update', array(
			'member' => $member,
      'forms'=>$forms,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Members::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='members-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
